package cn.faifai.desktop.viewModel;

import cn.faifai.common.utils.SpringContextUtil;
import cn.faifai.tagapi.dto.TagDTO;
import cn.faifai.tagapi.entity.Tag;
import cn.faifai.tagapi.repository.TagRepository;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import java.util.Date;

public class TagViewModel {

    // JavaFX Properties
    private ObjectProperty<Long> id = new SimpleObjectProperty<>();
    private ObjectProperty<Long> parentId = new SimpleObjectProperty<>();
    private StringProperty parentTagName = new SimpleStringProperty();
    private StringProperty name = new SimpleStringProperty();
    private ObjectProperty<Date> createdAt = new SimpleObjectProperty<>();
    private ObjectProperty<Date> lastModifiedAt = new SimpleObjectProperty<>();

    public TagViewModel() {}

    public TagViewModel(TagDTO tagDTO) {
        setId(tagDTO.getId());
        setParentId(tagDTO.getParentId());
        setName(tagDTO.getName());
        setCreatedAt(tagDTO.getCreatedAt());
        setLastModifiedAt(tagDTO.getLastModifiedAt());
        TagRepository tagRepository = SpringContextUtil.getContext().getBean(TagRepository.class);
        if (tagDTO.getParentId() != null) {
            String parentTag = tagRepository.findById(tagDTO.getParentId()).map(Tag::getName).orElse("根标签");
            setParentTagName(parentTag);
        }
    }

    public TagDTO toTagDTO() {
        TagDTO tagDTO = new TagDTO();
        tagDTO.setId(getId());
        tagDTO.setParentId(getParentId());
        tagDTO.setName(getName());
        tagDTO.setCreatedAt(getCreatedAt());
        tagDTO.setLastModifiedAt(getLastModifiedAt());
        return tagDTO;
    }

    // Getters and Setters for values
    public Long getId() {
        return id.get();
    }

    public void setId(Long id) {
        this.id.set(id);
    }

    public ObjectProperty<Long> idProperty() {
        return id;
    }

    public Long getParentId() {
        return parentId.get();
    }

    public void setParentId(Long parentId) {
        this.parentId.set(parentId);
    }

    public ObjectProperty<Long> parentIdProperty() {
        return parentId;
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public StringProperty nameProperty() {
        return name;
    }

    public Date getCreatedAt() {
        return createdAt.get();
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt.set(createdAt);
    }

    public ObjectProperty<Date> createdAtProperty() {
        return createdAt;
    }

    public Date getLastModifiedAt() {
        return lastModifiedAt.get();
    }

    public void setLastModifiedAt(Date lastModifiedAt) {
        this.lastModifiedAt.set(lastModifiedAt);
    }

    public ObjectProperty<Date> lastModifiedAtProperty() {
        return lastModifiedAt;
    }

    public String getParentTagName() {
        return parentTagName.get();
    }

    public void setParentTagName(String parentTagName) {
        this.parentTagName.set(parentTagName);
    }

    public StringProperty parentTagNameProperty() {
        return parentTagName;
    }
}